﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using FluorineFx.IO;

namespace TestTech
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 将数字由第一个进制转换成第二个进制
        /// </summary>
        /// <param name="inputNumber"></param>
        /// <param name="fromBase"></param>
        /// <param name="toBase"></param>
        /// <returns></returns>
        public string ConvertString(string inputNumber, int fromBase, int toBase)
        {
            int intValue = Convert.ToInt32(inputNumber, fromBase);
            return Convert.ToString(intValue, toBase);
        }
        /// <summary>
        /// 字符串转换成16进制的字符串，每个字节间包含空格
        /// </summary>
        /// <param name="input"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        private string StringToHex(string input, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            for (int i = 0; i < input.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append(" ");
                }
                if ((int)input[i] == zeroNum)
                {
                    sb.Append("00");
                }
                else
                {
                    sb.Append(Convert.ToString((int)input[i], 16).PadLeft(2, '0'));
                }
            }
            return sb.ToString().Trim();
        }
        /// <summary>
        /// 16进制还原成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <param name="zeroNum"></param>
        /// <returns></returns>
        private string HexToString(string hexData, int zeroNum = 0)
        {
            StringBuilder sb = new StringBuilder(100);
            hexData = hexData.Replace(" ", "");
            for (int i = 0; i < hexData.Length; i++)
            {
                if (i % 2 == 0)
                {
                    string tmp = hexData.Substring(i, 2);
                    if (tmp == "00")
                    {
                        sb.Append((char)zeroNum);
                    }
                    else
                    {
                        sb.Append((char)Convert.ToInt32(tmp, 16));
                    }
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 剔除16进制数据中的空格回车等，用于转换成字符串
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        private string RefineHex(string hexData)
        {
            return hexData.Replace(" ", "").Replace("\r", "").Replace("\n", "");
        }
        /// <summary>
        /// 将16进制的字符串转换成bytearray
        /// </summary>
        /// <param name="hexData"></param>
        /// <returns></returns>
        private byte[] HexStringToByteArray(string hexData)
        {
            hexData = this.RefineHex(hexData);
            int length = hexData.Length / 2;
            byte[] res = new byte[length];
            for (int i = 0; i < length; i++)
            {
                string tmp = hexData.Substring(i * 2, 2);
                tmp = ConvertString(tmp, 16, 10);
                res[i] = Convert.ToByte(tmp);
            }
            return res;
        }

        public void Login()
        {
            string gatway = "http://sg236.ledu.com/server/amfphp/gateway.php";
            string h = @"00 03 00 00 00 01 00 04 
6E 75 6C 6C 00 02 2F 32 00 00 00 F2 0A 00 00 00 
01 11 0A 81 13 4F 66 6C 65 78 2E 6D 65 73 73 61 
67 69 6E 67 2E 6D 65 73 73 61 67 65 73 2E 52 65 
6D 6F 74 69 6E 67 4D 65 73 73 61 67 65 0D 73 6F 
75 72 63 65 13 6F 70 65 72 61 74 69 6F 6E 11 63 
6C 69 65 6E 74 49 64 15 74 69 6D 65 54 6F 4C 69 
76 65 13 6D 65 73 73 61 67 65 49 64 17 64 65 73 
74 69 6E 61 74 69 6F 6E 13 74 69 6D 65 73 74 61 
6D 70 09 62 6F 64 79 0F 68 65 61 64 65 72 73 06 
0B 4C 6F 67 69 6E 06 29 67 65 74 4C 6F 67 69 6E 
41 6E 6E 6F 75 6E 63 65 6D 65 6E 74 01 04 00 06 
49 36 37 38 39 44 30 41 43 2D 44 30 45 45 2D 32 
32 41 33 2D 30 34 42 36 2D 30 46 33 44 36 32 44 
38 34 46 32 35 06 0D 61 6D 66 70 68 70 04 00 09 
03 01 04 00 0A 0B 01 15 44 53 45 6E 64 70 6F 69 
6E 74 01 09 44 53 49 64 06 07 6E 69 6C 01";
            h = this.RefineHex(h);

            byte[] ba = this.HexStringToByteArray(h);
            for (int i = 0; i < ba.Length; i++)
            {
                //Console.WriteLine(ba[i]);
            }
            Console.WriteLine(ConvertString("108",10,16));
            MemoryStream ms = new MemoryStream(ba);
            AMFDeserializer deserializer = new AMFDeserializer(ms);
            AMFMessage message = deserializer.ReadAMFMessage();

        }

    }
}
